package com.example.lj.uav.view.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.lj.uav.R;
import com.example.lj.uav.bean.RouteLine;
import com.example.lj.uav.utils.DoubleUtils;
import com.example.lj.uav.view.RouteSettingActivity;

import okhttp3.Route;

public class FragmentLineSetting extends Fragment implements View.OnClickListener {
    private TextView mTvSpeed;
    private TextView mTvAltitude;
    private ImageView mIvSpeedReduce;
    private ImageView mIvSpeedPlus;
    private ImageView mIvAltitudeReduce;
    private ImageView mIvAltitudePlus;
    private Spinner mSpAngle;
    private Spinner mSpControl;
    private Spinner mSpMotion;


    private RouteSettingActivity mActivity;
    private RouteLine line;
    private String[] angleIds = {"Auto","ControlledByRC","UsePointSetting"};//沿航线方向，手动控制，依照每个航点设置
    private String[] controlIds = {"UsePointSetting","ControlledByRC"};//依照每个航点设置，手动控制
    private String[] motionIds = {"Hover","GoHome","AutoLand","GoFirstPoint"};//悬停，自动返航，原地降落，返回航线起始点
    private String[] angles = {"沿航线方向","手动控制","依照每个航点设置"};
    private String[] controls = {"依照每个航点设置","手动控制"};
    private String[] motions = {"悬停","自动返航","原地降落","返回航线起始点"};
    private ArrayAdapter angleAdapter;
    private ArrayAdapter controlAdapter;
    private ArrayAdapter motionAdapter;
    private String angle;
    private String control;
    private String motion;
    private double speed;
    private double altitude;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_route_setting,null);
        mTvSpeed = view.findViewById(R.id.tv_line_speed);
        mTvAltitude = view.findViewById(R.id.tv_line_altitude);
        mIvSpeedReduce = view.findViewById(R.id.iv_line_speed_reduce);
        mIvSpeedPlus = view.findViewById(R.id.iv_line_speed_plus);
        mIvAltitudeReduce = view.findViewById(R.id.iv_line_altitude_reduce);
        mIvAltitudePlus = view.findViewById(R.id.iv_line_altitude_plus);
        mSpAngle = view.findViewById(R.id.sp_line_angle);
        mSpControl = view.findViewById(R.id.sp_line_control);
        mSpMotion = view.findViewById(R.id.sp_line_motion);
        mActivity = (RouteSettingActivity) getActivity();
        initView();
        return view;
    }

    public void getLineDataSuccess(RouteLine line) {
        this.line = line;
        angle = line.getHeadingMode();
        control = line.getGimbalPitchMode();
        motion = line.getActionOnFinish();
        speed = line.getSpeed();
        altitude = line.getAltitude();
    }

    private void initView() {
        mTvSpeed.setText(DoubleUtils.doubleFormat(line.getSpeed(),2)+"");
        mTvAltitude.setText(DoubleUtils.doubleFormat(line.getAltitude(),2)+"");
        //
        angleAdapter = new ArrayAdapter(mActivity,R.layout.textview_spinner_drop_down,angles);
        angleAdapter.setDropDownViewResource(R.layout.textview_spinner_drop_down);
        mSpAngle.setAdapter(angleAdapter);
        int anglePosition = 0;
        for (int i=0;i<angleIds.length;i++){
            if (angle.equals(angleIds[i])){
                anglePosition = i;
            }
        }
        mSpAngle.setSelection(anglePosition);
        mSpAngle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mActivity.getRouteLine().setHeadingMode(angleIds[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //
        controlAdapter = new ArrayAdapter(mActivity,R.layout.textview_spinner_drop_down,controls);
        controlAdapter.setDropDownViewResource(R.layout.textview_spinner_drop_down);
        mSpControl.setAdapter(controlAdapter);
        int controlPosition = 0;
        for (int i=0;i<controlIds.length;i++){
            if (control.equals(controlIds[i])){
                controlPosition = i;
            }
        }
        mSpControl.setSelection(controlPosition);
        mSpControl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mActivity.getRouteLine().setGimbalPitchMode(controlIds[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        //
        motionAdapter = new ArrayAdapter(mActivity,R.layout.textview_spinner_drop_down,motions);
        motionAdapter.setDropDownViewResource(R.layout.textview_spinner_drop_down);
        mSpMotion.setAdapter(motionAdapter);
        int motionPosition = 0;
        for (int i=0;i<motionIds.length;i++){
            if (motion.equals(motionIds[i])){
                motionPosition = i;
            }
        }
        mSpMotion.setSelection(motionPosition);
        mSpMotion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mActivity.getRouteLine().setActionOnFinish(motionIds[i]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        mIvSpeedReduce.setOnClickListener(this);
        mIvSpeedPlus.setOnClickListener(this);
        mIvAltitudeReduce.setOnClickListener(this);
        mIvAltitudePlus.setOnClickListener(this);
    }



    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_line_speed_reduce:
                if (speed<=0){
                    return;
                }
                speed = DoubleUtils.doubleFormat(speed-0.1,2);
                if (speed<=1){
                    speed = 1;
                }
                mActivity.getRouteLine().setSpeed(speed);
                mTvSpeed.setText(speed+"");
                break;
            case R.id.iv_line_speed_plus:
                speed = DoubleUtils.doubleFormat(speed+0.1,2);
                if (speed>=15){
                    speed = 15;
                }
                mActivity.getRouteLine().setSpeed(speed);
                mTvSpeed.setText(speed+"");
                break;
            case R.id.iv_line_altitude_reduce:
                if (altitude<=0){
                    return;
                }
                altitude = DoubleUtils.doubleFormat(altitude-0.1,2);
                mActivity.getRouteLine().setAltitude(altitude);
                mTvAltitude.setText(altitude+"");
                break;
            case R.id.iv_line_altitude_plus:
                altitude = DoubleUtils.doubleFormat(altitude+0.1,2);
                mActivity.getRouteLine().setAltitude(altitude);
                mTvAltitude.setText(altitude+"");
                break;
        }
    }
}
